package com.geekchique.runningbuddy.util;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.geekchique.runningbuddy.model.JogEntry;
import com.geekchique.runningbuddy.provider.JogsDBSQL;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper {

    private SQLiteDatabase db;

    public DBHelper(Context ctx) {
    	
        try {
        	db = ctx.openDatabase(JogsDBSQL.DATABASE_NAME, null);
            
        } catch (FileNotFoundException e) {
            createJoggingEntriesDB(ctx);
        }
    }

	public void createJoggingEntriesDB(Context ctx) {
		try {
		    db = ctx.createDatabase(JogsDBSQL.DATABASE_NAME, JogsDBSQL.DATABASE_VERSION, 0, null);
		    db.execSQL(JogsDBSQL.DATABASE_CREATE);
		} catch (FileNotFoundException e1) {
		    db = null;
		}
	}
    
    public void close() {
        db.close();
    }

    public void addJogEntries(JogEntry entry) {
        ContentValues initialValues = new ContentValues();
        
	        initialValues.put("name", entry.name);
	        initialValues.put("startTime",entry.startTime);
	        initialValues.put("stopTime", entry.stopTime);
	        initialValues.put("longitude", entry.longitude);
	        initialValues.put("latitude", entry.latitude);
	        initialValues.put("cellTowerId", entry.cellTowerId);
	        initialValues.put("locationAreaCode", entry.locationAreaCode);
	        db.insert(JogsDBSQL.DATABASE_TABLE1, null, initialValues);
    }
    
    public void deleteJogEntry(long Id) {
        db.delete(JogsDBSQL.DATABASE_TABLE1, "id=" + Id, null);
    }

    public List<JogEntry> fetchAllJogEntries(){
        ArrayList<JogEntry> ret = new ArrayList<JogEntry>();
      
        Cursor c =
            db.query(JogsDBSQL.DATABASE_TABLE1, new String[] {
                "_id",
                "name",
                "startTime",
                "stopTime",
                "longitude",
                "latitude",
                "cellTowerId",
                "locationAreaCode"
                },
                null, null, null, null, null);
        int numRows = c.count();
        c.first();
        for (int i = 0; i < numRows; ++i) {
        	JogEntry row = new JogEntry();
            row._id = c.getLong(0);
            
            row.name = c.getString(1);
            row.startTime = c.getLong(2);
            row.stopTime = c.getLong(3);
            row.longitude = c.getLong(4);
            row.latitude = c.getLong(5);
            row.cellTowerId = c.getInt(6);
            row.locationAreaCode = c.getInt(7);
            
            ret.add(row);
            c.next();
        }
        
        return ret;
    }
    
    

    public JogEntry fetchJogEntry(long Id) {
        JogEntry row = new JogEntry();
        Cursor c =
        db.query(true, JogsDBSQL.DATABASE_TABLE1, new String[] {
                "_id", "name", "startTime", "stopTime", "longitude", "latitude", "cellTowerId", "locationAreaCode"},
                "Id=" + Id, null, null, null, null);
        
        if (c.count() > 0) {
            c.first();
            row._id = c.getLong(0);
            
            row.name = c.getString(1);
            row.startTime = c.getLong(2);
            row.stopTime = c.getLong(3);
            row.longitude = c.getLong(4);
            row.latitude = c.getLong(5);
            row.cellTowerId = c.getInt(6);
            row.locationAreaCode = c.getInt(7);
            
            return row;
        } else {
            row._id = -1;
            row.longitude = -1;
            row.name = null;
        }
        return row;
    }

    public void updateJogEntry(long Id, JogEntry entry) {
        ContentValues args = new ContentValues();
        args.put("_id", entry._id);
        args.put("name", entry.name);
        args.put("startTime", entry.startTime);
        args.put("stopTime", entry.stopTime);
        args.put("longitude", entry.longitude);
        args.put("latitude", entry.latitude);
        args.put("cellTowerId", entry.cellTowerId);
        args.put("locationAreaId", entry.locationAreaCode);
        
        db.update(JogsDBSQL.DATABASE_TABLE1, args, "id=" + Id, null);
    }

	public Cursor getCursorOfAllJogs() {
		return db.query(JogsDBSQL.DATABASE_TABLE1, JogEntry.COLUMNS_IN_JOG_ENTRY, null, null, null, null, null);
	}
}



